// Created by wangliang on 2025/4/12.
import 'dart:io';

import 'package:device_info_plus/device_info_plus.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:uuid/uuid.dart';

class AppHelper {
  AppHelper._();

  static final AppHelper _instance = AppHelper._();

  static AppHelper get instance => _instance;

  static const String _distinctIdKey = "distinct_id_key";
  static const String _deviceCodeKey = "device_code_key";
  static const String _activeAppKey = "active_app_key";
  static const String _showRegisterRewardKey = "show_register_reward_key";

  String get platformId => Platform.isAndroid
      ? '1'
      : Platform.isIOS
          ? '2'
          : '';

  String _appVersion = '';

  Future<String> get appVersion async {
    if (_appVersion.isNotEmpty) {
      return _appVersion;
    }
    final info = await PackageInfo.fromPlatform();
    _appVersion = info.version;
    return _appVersion;
  }

  String _deviceModel = '';
  Future<String> get deviceModel async {
    if (_deviceModel.isNotEmpty) {
      return _deviceModel;
    }
    final deviceInfo = DeviceInfoPlugin();

    if (Platform.isAndroid) {
      final androidInfo = await deviceInfo.androidInfo;
      _deviceModel = androidInfo.model;
    } else if (Platform.isIOS) {
      final iosInfo = await deviceInfo.iosInfo;
      _deviceModel = iosInfo.utsname.machine;
    } else {
      _deviceModel = '';
    }
    return _deviceModel;
  }

  String _distinctId = '';
  Future<String> getDistinctId() async {
    if (_distinctId.isNotEmpty) {
      return _distinctId;
    }
    var prefs = await SharedPreferences.getInstance();
    _distinctId = prefs.getString(_distinctIdKey) ?? '';
    if (_distinctId.isEmpty) {
      var uuid = const Uuid();
      _distinctId = uuid.v4();
      prefs.setString(_distinctIdKey, _distinctId);
    }
    return _distinctId;
  }

  Future<bool> isActiveApp() async {
    var prefs = await SharedPreferences.getInstance();
    return prefs.getBool(_activeAppKey) ?? false;
  }

  Future setActiveApp() async {
    var prefs = await SharedPreferences.getInstance();
    prefs.setBool(_activeAppKey, true);
  }

  String _deviceCode = '';
  Future<String> getDeviceCode() async {
    if (_deviceCode.isNotEmpty) {
      return _deviceCode;
    }
    var prefs = await SharedPreferences.getInstance();
    _deviceCode = prefs.getString(_deviceCodeKey) ?? '';
    if (_deviceCode.isEmpty) {
      if (Platform.isAndroid) {
        final DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
        final androidInfo = await deviceInfo.androidInfo;
        _deviceCode = androidInfo.id; // or androidInfo.androidId (requires READ_PRIVILEGED_PHONE_STATE)
      } else if (Platform.isIOS) {
        final DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
        final iosInfo = await deviceInfo.iosInfo;
        _deviceCode = iosInfo.identifierForVendor ?? '';
      } else {
        return '';
      }
      prefs.setString(_deviceCodeKey, _deviceCode);
    }
    return _deviceCode;
  }

  Future setNeedShowRegisterReward() async {
    var prefs = await SharedPreferences.getInstance();
    prefs.setBool(_showRegisterRewardKey, true);
  }

  Future setShownRegisterReward() async {
    var prefs = await SharedPreferences.getInstance();
    prefs.setBool(_showRegisterRewardKey, false);
  }

  Future<bool> needShowRegisterReward() async {
    var prefs = await SharedPreferences.getInstance();
    return prefs.getBool(_showRegisterRewardKey) ?? false;
  }

  Future bindPushDevice() async {
    // String pushDeviceToken = await RuiXueApi().getPushDeviceToken();
    // if (!Platform.isIOS) {
    //   Log.d('bindPushDevice is not ios platform, so do nothing.');
    //   return;
    // }
    // if (pushDeviceToken.isEmpty) {
    //   Log.d('bindPushDevice pushDeviceToken is empty, so do nothing.');
    //   return;
    // }
    // try {
    //   if (AccountHelper.instance.isLogin()) {
    //     await CommonApi.bindDevice(pushDeviceToken, 'apns');
    //   } else {
    //     await CommonApi.bindDeviceUnLogin(pushDeviceToken, 'apns');
    //   }
    // } on ApiException catch (e) {
    //   Log.d('bindPushDevice error $e');
    // }
  }

  Future unbindPushDevice() async {
    // if (!Platform.isIOS) {
    //   Log.d('unbindPushDevice is not ios platform, so do nothing.');
    //   return;
    // }
    // try {
    //   await CommonApi.unbindDevice();
    // } on ApiException catch (e) {
    //   Log.d('unbindPushDevice error $e');
    // }
  }

  String getOSName() => Platform.isAndroid ? 'android' : 'ios';
  Future<String> getOSVersion() async {
    if (Platform.isAndroid) {
      var androidInfo = await DeviceInfoPlugin().androidInfo;
      return androidInfo.version.release;
    } else if (Platform.isIOS) {
      return Platform.operatingSystemVersion;
    } else {
      return '';
    }
  }
}
